define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'scanwork/qrcode/index' + location.search,
                    add_url: 'scanwork/qrcode/add',
                    edit_url: 'scanwork/qrcode/edit',
                    del_url: 'scanwork/qrcode/del',
                    multi_url: 'scanwork/qrcode/multi',
                    table: 'qrcode',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'Id', sortable: true},
                        {field: 'allocation.order.order_no', title: '订单号', align: 'left'},
                        {field: 'allocation.model.product.name', title: '产品名称', align: 'left'},
                        {field: 'allocation.model.name', title: '型号名称', align: 'left'},
                        {field: 'allocation.process.name', title: '工序名称', align: 'left'},
                        {field: 'allocation.user.nickname', title: '员工', align: 'left'},
                        {field: 'allocation.quantity', title: '任务数量', align: 'center'},
                        {field: 'scan_count', title: '扫码次数', align: 'center'},
                        {field: 'status', title: '状态', searchList: {"0":'未使用',"1":'已使用'}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: '生成时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: '操作', table: table, events: {
                            'click .btn-print-qrcode': function (e, value, row, index) {
                                var img = row.qr_image;
                                var win = window.open('');
                                win.document.write('<img src="' + img + '" style="width:300px;height:300px;">');
                                win.print();
                            }
                        }, formatter: function(value, row, index) {
                            var html = '';
                            if (row.qr_image) {
                                html += '<a href="' + row.qr_image + '" class="btn btn-xs btn-info" target="_blank"><i class="fa fa-eye"></i> 查看</a> ';
                                html += '<a href="' + row.qr_image + '" class="btn btn-xs btn-success" download><i class="fa fa-download"></i> 下载</a> ';
                                html += '<a href="javascript:;" class="btn btn-xs btn-warning btn-print-qrcode" data-img="' + row.qr_image + '"><i class="fa fa-print"></i> 打印</a> ';
                            }
                            return html;
                        }}
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        batchGenerate: function () {
            // 批量生成二维码功能
            var table = $("#table");
            
            // 添加批量生成按钮到工具栏
            var toolbar = $('.toolbar');
            if (toolbar.length === 0) {
                toolbar = $('<div class="toolbar" style="margin-bottom:10px;"></div>').prependTo(table.closest('.fixed-table-container').length ? table.closest('.fixed-table-container') : table.parent());
            }
            toolbar.html('<button type="button" class="btn btn-success btn-batch-generate"><i class="fa fa-qrcode"></i> 批量生成二维码</button>');
            
            // 批量生成事件
            toolbar.on('click', '.btn-batch-generate', function(){
                var ids = table.bootstrapTable('getSelections').map(function(row){return row.id;});
                if(ids.length === 0){ 
                    Toastr.error('请先选择要生成二维码的数据'); 
                    return; 
                }
                
                // 跳转到批量生成页面
                Layer.open({
                    type: 2,
                    title: '批量生成二维码',
                    shadeClose: true,
                    shade: 0.3,
                    maxmin: true,
                    area: ['900px', '700px'],
                    content: 'scanwork/qrcode/batchGenerate?ids=' + ids.join(',') + '&dialog=1',
                    success: function(layero, index) {
                        // 弹窗打开成功
                    },
                    end: function() {
                        // 弹窗关闭后刷新表格
                        table.bootstrapTable('refresh');
                    }
                });
            });
            
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
}); 